<?php
namespace app\admin\controller;

class Goodscat extends Base
{
    //列表
    public function lists(){
        if(is_post()){
            $d = $this->post;
            $map = [];
            if($d['keyword']!=''){ $map[] = ['and','a.cat_title','like',"%{$d['keyword']}%"]; }
            if($d['cat_id']!=''){ $map[] = ['and','a.parent_id','=',$d['cat_id']]; }
            if($d['level']!=''){ $map[] = ['and','a.level','=',$d['level']]; }
            $w  = where($map);
            $rs = $this->db->query("select a.*,b.cat_title ptitle from goods_cat a left join goods_cat b on a.parent_id = b.id $w[0] order by a.sort desc,a.id desc".limit($d['page'],$d['limit']), $w[1],'',true);
            if($rs){
                $rule = [
                    'create_time' => 'com_time_format',
                    'status'=>function($v){
                         return $v==0?'显示':'隐藏';
                    },
                ];
                $data = com_format($rs[0],$rule);
                suc($data,$rs[1]);
            }else{
                suc([],0);
            }
            $this->fetch();
        }
        $cat = m('GoodsCat','c')->allSubCat();
        return $this->fetch(['cat'=>$cat]);
    }

    //添加
    public function add(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'parent_id'=>'require',
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['cat_tree'] = m('GoodsCat','c')->buildTree($d['parent_id']);
            $d['create_time'] = time();
            unset($d['file']);
            if(isset($d['specs'])){
                $d["goods_spec_ids"] = join(",",$d['specs']);
            }else{
                $d["goods_spec_ids"] = "";
            }
            unset($d['specs']);
            $id = m('GoodsCat','c')->add($d);
            m('Attachment','xcom')->imgSure('goods_cat',$id,$d);
            if ($id) {
                cookie('js_msg','添加成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('添加失败');
            }
        }
        $all= [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'depath' => 0,
            ]
        ];
        //查询规格
        $spec_list = $this->db->query("select  * from goods_spec where status = ?",[0]);
        $cat = $all + m('GoodsCat','c')->allSubCat();
        return $this->fetch(['cat'=>$cat,'spec_list'=>$spec_list]);
    }

    //编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'parent_id'=>'require',
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);

            $info = $this->db->find('goods_cat',$d['id']);
            $d['cat_tree'] = m('GoodsCat','c')->buildTree($d['parent_id']);
            $d['update_time'] = time();
            unset($d['file']);
            if(isset($d['specs'])){
                $d["goods_spec_ids"] = join(",",$d['specs']);
            }else{
                $d["goods_spec_ids"] = "";
            }
            unset($d['specs']);
            $id = $this->db->update('goods_cat',$d);
            m('GoodsCat','c')->editLevel($d['cat_tree']);
            m('Attachment','xcom')->imgSure('goods_cat',$d['id'],$d);
            if ($id) {
                $old_tree = $info['cat_tree'].$info['id'].',';
                $new_tree = $d['cat_tree'].$info['id'].',';
                $this->db->query("update goods_cat set cat_tree=REPLACE(cat_tree,'{$old_tree}','{$new_tree}') where cat_tree like ?",[$old_tree.'%']);
                m('GoodsCat','c')->editLevel($new_tree);
                cookie('js_msg','修改成功');
                cookie('js_function','preload');
                return suc();
            }else{
                err('修改失败');
            }
        }
        $all= [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'cat_tree' => ',0,',
                'depath' => 0,
            ]
        ];
        $cat = $all + m('GoodsCat','c')->allSubCat();
        $id = $this->get['id'];
        $info = $this->db->query("select * from goods_cat where id=?",[$id],1);
        $info['goods_spec_ids'] = explode(",",$info["goods_spec_ids"]);
        //干掉自身及以下的分类
        $cat_tree = $info['cat_tree'].$info['id'].",";
        foreach($cat as $k => $v){
            if( strpos($v['cat_tree'], $cat_tree) === 0 || $v['id']==$info['id']){
                unset($cat[$k]);
            }
        }
        //查询规格
        $spec_list = $this->db->query("select  * from goods_spec where status = ?",[0]);
        return $this->fetch(['cat'=>$cat,'info'=>$info,'spec_list'=>$spec_list]);
    }
    //删除
    public function del(){
        if(is_post()){
            $d = $this->post;
            $rs = $this->db->query("select id from goods_cat where parent_id = ? limit 1",[$d['id']],1);
            if ($rs) err('该分类下有子分类,不能被删除!');

            $res = $this->db->query("select id from goods where cat_id = ? limit 1",[$d['id']],1);
            if ($res) err('分类下有文章，不能删除!');

            $this->db->delete('goods_cat',$d['id']);
            m('Attachment','xcom')->imgDel('goods_cat',$d['id'],true);
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
        }
    }

    #批量删除
    function delall(){
      if(is_post()){
            $d = $this->post;
            $ids = $d['id'];
            foreach($ids as $id){
                $rs = $this->db->query("select id from goods_cat where parent_id = ? limit 1",[$id],1);
                if ($rs) err('该分类下有子分类,不能被删除!');

                $res = $this->db->query("select id from goods where cat_id = ? limit 1",[$id],1);
                if ($res) err('分类下有产品，不能删除!');

                $this->db->delete('goods_cat',$id);
                m('Attachment','xcom')->imgDel('goods_cat',$id,true);
            }
            cookie('js_msg','删除成功');
            cookie('js_function','close|reload');
            return suc();
      }
    }

    //获取子分类
    public function getsub(){
        if(is_post()){
            $d = $this->post;
            $rs = $this->db->query('select id,cat_title from goods_cat where parent_id=?',[$d['pid']]);
            $rs = array_column($rs,'cat_title','id');
            suc($rs);
        }
    }

    //详情
    public function detail($id){
        return $this->fetch();
    }
}
